﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponents.Data.SqlCe;
using ProgNetComponentsDx.Browsers;
using ProgNetComponents.Ini;
using ProgNetComponentsDx.Layout;
using ProgNetComponentsDx.Options;


namespace Lottomat.Init
{
    public partial class InitForm : ProgNetComponentsDx.Forms.BaseInitForm
    {
        public InitForm()
        {
            InitializeComponent();
            this.lblTitle.Text = Application.ProductVersion;
            this.Text = Application.ProductName;
        }

        ProgNetComponents.Data.SqlCe.SqlCeDataManager helper = new ProgNetComponents.Data.SqlCe.SqlCeDataManager();

        protected override void PreInit()
        {
            base.PreInit();
            BrowserCache.SaveLayouts = true;
            BrowserCache.BeforeBrowserShowDialog += new BrowserHandler(BrowserCache_BeforeBrowserShowDialog);

            SetProgressText("Inicjalizacja bazy danych.");
            IniFile iniFile = new IniFile(Application.StartupPath + "\\Lottomat.ini");
            iniFile.ReadIni();
            string dbFile = iniFile.GetValueFormCache("Main", "DB");
            if (dbFile.IsNullOrEmpty())
                dbFile = Application.StartupPath + "\\Lottomat.sdf";

            ProgNetComponents.Data.SqlCe.SqlCeDataManager.GlobalConnectionString = "Data Source=" + dbFile + "; Encrypt Database=False; Max Database Size = 4091; Default Lock Timeout = 10000"; //File Mode=Read Write;
            int lastAction = 0;
            if (System.IO.File.Exists(dbFile))
            {
                try
                {
                    object obj = helper.ExecuteScalar("SELECT STP_OP_WARTOSC FROM STP_OPCJE WHERE STP_OP_NAZWA = 'DB_UPDATE'");
                    if (obj != null)
                    {
                        lastAction = obj.AsInt();
                    }
                }
                catch { }
            }
            AppUpdate.Update(lastAction);
        }

        void BrowserCache_BeforeBrowserShowDialog(BrowserEventArgs e)
        {
            //if (Init.InitFormBrowserNames.Setup.Srodowiska.IsTheSameKey(e.BrowserId))
            //{
            //    if (OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Setup_Group.Definicje_Group.Srodowiska.UpdateOnOpen))
            //    {
            //        Setup.Srodowiska.Tools.UpdateAllEnvs(false, null, false);
            //    }
            //}
        }

        protected override ProgNetComponentsDx.Data.SelectDataManagerCreator CreateDataManagerCreator()
        {
            return new ProgNetComponentsDx.Data.SqlCeSelectDataManagerCreator();
        }
        protected override ProgNetComponents.Data.DataManager CreateDataManagerForBrowsers()
        {
            SqlCeDataManager g = new SqlCeDataManager();
            g.AutoLogExceptions = true;
            return g;
        }
        protected override LayoutGrabber CreateLayoutGrabber()
        {
            SqlCeLayoutGrabber g = new SqlCeLayoutGrabber();
            g.Manager.AutoLogExceptions = true;
            g.TableName = "STP_LAYOUT";
            g.XmlColumn = "STP_LAYOUT_XML_IMG";
            g.LayoutNameColumn = "STP_LAYOUT_NAZWA";
            g.ControlNameColumn = "STP_LAYOUT_KONTROLKA";
            return g;
        }
        protected override OptionGrabber CreateOptionGrabber()
        {
            SqlCeOptionGrabber g = new SqlCeOptionGrabber();
            g.Manager.AutoLogExceptions = true;
            g.TableName = "STP_OPCJE";
            g.OptionNameColumn = "STP_OP_NAZWA";
            g.OptionValueColumn = "STP_OP_WARTOSC";
            return g;
        }
        protected override ProgNetComponentsDx.Shortcut.ShortcutGrabber CreateShotrcutsGrabber()
        {
            ProgNetComponentsDx.Shortcut.SqlCeShortcutGrabber g = new ProgNetComponentsDx.Shortcut.SqlCeShortcutGrabber();
            g.ShortcutNameColumn = "STP_SC_NAZWA";
            g.ShortcutValueColumn = "STP_SC_WARTOSC";
            g.TableName = "STP_SHORTCUT";
            return g;
        }
        protected override void InitOptions()
        {
            base.InitOptions();
            OptionCache.GetOption(InitFormOptionNames.Wyglad.Skorka).DefaultValue = this.LookAndFeel.ActiveSkinName;
            OptionCache.GetOption(InitFormOptionNames.Wyglad.Styl).DefaultValue = this.LookAndFeel.Style;
        }
        
        protected override void InitLangouage()
        {
            base.InitLangouage();
            //ProgNetComponentsDx.Language.ProgNetLanguage.SetPLLocalizer();
            ProgNetComponentsDx.Language.ProgNetLanguage pl = new ProgNetComponentsDx.Language.ProgNetLanguage(new System.Globalization.CultureInfo("pl-PL"));
            pl.RegisterManager("DB", Lng.DB.ResourceManager);
            ProgNetComponentsDx.Language.ProgNetLanguage.LogMissingValues = true;
            ProgNetComponentsDx.Language.ProgNetLanguage.CurrentLanguageCode = "pl-PL";
        }

    }
}
